package com.mediacloud.app.newsmodule.fragment.baoliao.activity;

import static com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig.getAppServerConfigInfo;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.GsonUtils;
import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.model.activity.BaseBackActivity;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.frag.BaoLiaoFragment;
import com.mediacloud.app.newsmodule.fragment.baoliao.model.DraftModel;
import com.mediacloud.app.newsmodule.utils.LoginUtils;
import com.mediacloud.app.reslib.config.NewsDetailStyleConfig;
import com.mediacloud.app.user.model.UserInfo;
import com.tencent.mmkv.MMKV;

import java.util.LinkedHashSet;

public class BaoLiaoActivity extends BaseBackActivity {

    BaoLiaoFragment fragment;

    public static void startActivity(Context context, String title, String catalogId) {
        startActivityV2(context, -1, title, catalogId);
    }

    public static void startActivityV2(Context context, int requestCode, String title, String catalogId) {
        if (!UserInfo.isLogin(context)) {
            LoginUtils.invokeLogin(context, "爆料", null, true, requestCode);

            //LoginUtils.invokeLoginV2(context,requestCode);
            return;
        }
        Intent intent = new Intent();
        intent.setClass(context, BaoLiaoActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("catalogId", catalogId);
        if (context instanceof Activity) {
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
    }


    @Override
    public int getLayoutResID() {
        return R.layout.activity_bao_liao;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //爆料统一白底黑字
        LinkedHashSet<String> linkedHashSet = getAppServerConfigInfo(BaoLiaoActivity.this).getNewsDetailConfig().getLinkedHashSet();
        if (!linkedHashSet.contains(NewsDetailStyleConfig.COLLECT_BUTTON) && !linkedHashSet.contains(NewsDetailStyleConfig.SHARE_BUTTON)) {
            mTitlebar_MoreContainer.setVisibility(View.VISIBLE);
        } else {
            mTitlebar_MoreContainer.setVisibility(View.GONE);
        }
//        mMoreText.setVisibility(View.VISIBLE);
//        mTitlebar_More.setVisibility(View.GONE);
//        setMoreText("发布");
//        mBackText.setText("取消");
//        mBackText.setVisibility(View.VISIBLE);
//        mTop_back.setVisibility(View.GONE);
        String title = getIntent().getStringExtra("title");
        title = TextUtils.isEmpty(title) ? getResources().getString(R.string.baoliao_i_want) : title;
        setTitle(title);
        if (mTitlebar_name != null) {
            mTitlebar_name.getPaint().setFakeBoldText(false);
            mTitlebar_name.setIncludeFontPadding(false);
            mTitlebar_name.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.dimen20sp));
        }
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragment = new BaoLiaoFragment();
        fragment.setArguments(getIntent().getExtras());
        transaction.add(R.id.baoliao_container, fragment);
        transaction.commitNowAllowingStateLoss();
    }

    @Override
    public int getStatusBarColor() {
        return Color.WHITE;
    }

    @Override
    protected void moreClicked() {
        if (fragment != null) {
            if (FunKt.isFastClick())
                fragment.onSubmit();
        }
    }


    @Override
    protected String getContent_show_top_color() {
        mTitileBar.setBackgroundColor(Color.WHITE);
        return "#333333";
    }

    @Override
    protected void backHandle() {
        tipDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            tipDialog();
        }
        return false;
    }

    private void tipDialog() {
        if (!fragment.isBaoLiu()) {
            MMKV.defaultMMKV().remove("baoliao_draft");
            finish();
        } else {
            Dialog dialog = new Dialog(this, R.style.CustomDatePickerDialog);
            //2、设置布局
            View view = View.inflate(this, R.layout.popup_window_view_exit, null);
            //拿到两个按钮
            TextView tipTitle = view.findViewById(R.id.Te_pop_name);
            TextView tipYes = view.findViewById(R.id.Te_yes);
            TextView tipNo = view.findViewById(R.id.Te_no);
            tipTitle.setText("将保留此次编辑");
            tipYes.setText("保留");
            tipNo.setText("不保留");
            dialog.setContentView(view);

            tipYes.setOnClickListener(view1 -> {
                fragment.seveDraft();
                dialog.cancel();
                finish();
            });
            tipNo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fragment.deleteDraft();
                    dialog.cancel();
                    finish();
                }
            });
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            Window window = dialog.getWindow();
            //设置弹出位置
            window.setGravity(Gravity.CENTER);
            //设置弹出动画
//        window.setWindowAnimations(R.style.dialogstyle);
            //设置对话 框大小
            window.setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            );
            dialog.show();

        }


    }
}